package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.Region;
import com.cskaoyan.bean.RegionExample;
import com.cskaoyan.bean.admin.vo.RegionT1InfoVO;
import com.cskaoyan.bean.admin.vo.RegionT2InfoVO;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jcy
 */
@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    RegionMapper regionMapper;

    /**
     *商场管理
     * 行政区域
     * @return
     */
    @Override
    public BasePageData list() {
        List<RegionT1InfoVO> regionT1InfoVOS = new ArrayList<>();
        //先查找省级地区
        RegionExample exampleT1 = new RegionExample();
        RegionExample.Criteria criteriaT1 = exampleT1.createCriteria();
        criteriaT1.andTypeEqualTo(((byte) 1));
        List<Region> regionsT1 = regionMapper.selectByExample(exampleT1);
        //遍历所有省级地区，查找每个省级地区对应的市级地区
        for (Region regionT1 : regionsT1) {
            RegionExample exampleT2 = new RegionExample();
            RegionExample.Criteria criteriaT2 = exampleT2.createCriteria();
            criteriaT2.andPidEqualTo(regionT1.getId()).andTypeEqualTo(((byte) 2));
            List<Region> regionsT2 = regionMapper.selectByExample(exampleT2);
            //用来封装该省级地区的市级地区
            List<RegionT2InfoVO> regionT2InfoVOS = new ArrayList<>();
            //再遍历市级地区，查找每个市级地区对应的县级地区
            for (Region regionT2 : regionsT2) {
                RegionExample exampleT3 = new RegionExample();
                RegionExample.Criteria criteriaT3 = exampleT3.createCriteria();
                criteriaT3.andPidEqualTo(regionT2.getId()).andTypeEqualTo(((byte) 3));
                List<Region> regionsT3 = regionMapper.selectByExample(exampleT3);
                //将该市级地区的县级地区添加到List
                regionT2InfoVOS.add(new RegionT2InfoVO(regionT2.getId(),regionT2.getName(),regionT2.getType(),regionT2.getCode(),regionsT3));
            }
            //将该省级地区的市级地区添加到List
            regionT1InfoVOS.add(new RegionT1InfoVO(regionT1.getId(),regionT1.getName(),regionT1.getType(),regionT1.getCode(),regionT2InfoVOS));
        }
        return BasePageData.ok(regionT1InfoVOS);
    }

    @Override
    public List<Region> queryById(Integer pid) {

        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        criteria.andPidEqualTo(pid);


        List<Region> regions = regionMapper.selectByExample(regionExample);

        return regions;
    }
}
